package com.study.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.vo.CartBox;
import com.study.services.DeportService;
import com.study.services.GoodsService;
import com.study.util.Times;

/**
 * 库存控制层
 * 
 * @author 郭星
 *
 */
@Controller
public class DeportController {
	@Autowired
	DeportService deportService;
	@Autowired
	GoodsService goodsService;
	
	////////////////////////////
	////////////////////////库存
	/**
	 * 访问地址http://localhost:8089/deportitem
	 * 跳转到库存列表页面
	 * @return
	 *
	 */
	@RequestMapping("/deportitem")
	public String first(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		System.out.println("我来时来了");
		return "depot/deportitem";
	}
	
	/**
	 * 分页查询
	 */
	@PostMapping("/depot/deportitemse")
	@ResponseBody
	public String getForm1(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectByPagerSql("", "", "", 1, 10, uid);
		Times.formatDate(list,"cproducetime");

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * deportitemse2分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/depot/deportitemse2")
	@ResponseBody
	public String getItem(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectByPagerSql(selects, selectinput, selectend, currentPage, pagesize, uid);

		Times.formatDate(list,"cproducetime");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		
		return json;
	}
	
	

	////////////////////////////
	////////////////////////入库
	/**
	 * 访问地址http://localhost:8089/deportitem
	 * 跳转到入库列表页面
	 * @return
	 *
	 */
	@RequestMapping("/instockes")
	public String instockes(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		System.out.println("进入instockes页面");
		return "depot/instockes";
	}
	
	
	/**
	 * 访问地址http://localhost:8089/deportitem
	 * 跳转到入库列表页面
	 * @return
	 *
	 */
	@RequestMapping("/instockesse")
	public String instockes2(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		System.out.println("进入\"/depot/instockesdrog\"页面");
		return "/depot/instockesdrog";
	}
	
	/**
	 * 传入新建入库ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/instockesdilog")
	public ModelAndView instockesdilog(HttpSession session) {
		ModelAndView mv = new ModelAndView("/depot/instockesdrog");
		System.out.println("instockesdilog:iframe");
		//查询对应的仓库
		int uid = (int) session.getAttribute("useid");
		mv.addObject("shelfs", deportService.findDeportShelf(uid));
		return mv;
	}
	
	
	/**
	 * 传入编辑入库ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/instockesdilogup")
	public ModelAndView instockesdilog(Integer ids,HttpSession session) {
		System.out.println("instockesdetaildrogup:iframe...."+ids);
		ModelAndView mv = new ModelAndView("/depot/instockesdetaildrogup");
		//查询对应的仓库
		int uid = (int) session.getAttribute("useid");
		mv.addObject("shelfs", deportService.findDeportShelf(uid));
		
		
		JSONObject findInstock = deportService.findInstock(ids);
		List<JSONObject> list = new ArrayList<>();
		list.add(findInstock);

		Times.formatDate(list,"tdate");
		Times.formatDate(list,"Idate");
		mv.addObject("textInstock", list.get(0));
		
		
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		if (cart == null) {
			cart = new CartBox();
		}
		deportService.findinstockDetail(ids, cart);
		session.setAttribute("ingInOid", ids);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		//System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		//session.setAttribute("CartBoxInAll", cart);
		//编辑时也要查看明细
		session.setAttribute("CartBoxInthisJson", jsonObj);
		return mv;
	}
	
	/**
	 *入库ifram表单详略信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/instockesdilogup0")
	public ModelAndView instockesdilog0(Integer ids,HttpSession session) {
		System.out.println("instockesdetaildrog:iframe...."+ids);
		ModelAndView mv = new ModelAndView("/depot/instockesdetaildrog");
		//查询对应的仓库
		int uid = (int) session.getAttribute("useid");
		mv.addObject("shelfs", deportService.findDeportShelf(uid));
		
		
		JSONObject findInstock = deportService.findInstock(ids);
		List<JSONObject> list = new ArrayList<>();
		list.add(findInstock);

		Times.formatDate(list,"tdate");
		Times.formatDate(list,"Idate");
		mv.addObject("textInstock", list.get(0));
		
		
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		if (cart == null) {
			cart = new CartBox();
		}
		deportService.findinstockDetail(ids, cart);
		session.setAttribute("ingInOid", ids);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		//System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		//session.setAttribute("CartBoxInAll", cart);
		//编辑时也要查看明细
		session.setAttribute("CartBoxInxlJson", jsonObj);
		
		return mv;
	}
	
	/**
	 * 添加一个新入库单
	 * 
	 * @param session
	 * @return
	 */
	@PostMapping("/addinstockes")
	@ResponseBody
	public JSONObject addinstockesdilog(String titles,Integer deport,Date havetime,String descins,HttpSession session) {
		System.out.println("添加一条入库单,主题："+titles+"仓库："+deport+"填单时间："+havetime+"备注："+descins);

		//新建入库单
		String uid = (int) session.getAttribute("useid")+"";
		int addInstock = deportService.addInstock(titles, deport, havetime, descins,uid);
		JSONObject json = new JSONObject();
		json.put("addinstok", addInstock);
		return json;
	}
	
	/**
	 * 从添加新入库单跳转的页面
	 * @param session
	 * @return
	 */
	@RequestMapping("/instockesnew")
	public ModelAndView instockestonew(int fils,HttpSession session) {
		ModelAndView mv = new ModelAndView("/depot/instockes");
		System.out.println("从添加新入库单跳转的页面");
		mv.addObject("shelfs", fils);
		return mv;
	}
	
	/**
	 * 修改一个入库单
	 * 
	 * @param session
	 * @return
	 */
	@PostMapping("/updateinstockes")
	@ResponseBody
	public JSONObject updateinstockesdilog(String titles,Integer deport,Date havetime,String descins,String did,HttpSession session) {
		System.out.println("修改一条入库单,主题："+titles+"仓库："+deport+"填单时间："+havetime+"备注："+descins+"did:"+did);

		//修改入库单
		int addInstock = deportService.updateInstock(titles, deport, havetime, descins, did);
		JSONObject json = new JSONObject();
		json.put("upinstok", addInstock);
		return json;
	}
	
	/**
	 *  修改入库单里指定入库详情的数量和备注
	 * @return
	 */
	@PostMapping("/updateDetailQuandt")
	@ResponseBody
	public int upddateDetailQuatitysText(Integer quatitys,String desctext,Integer gidse,Integer idse ) {
		System.out.println("进入了修改入库单里指定入库详情的数量和备注这个界面:"+quatitys+"  "+desctext+" "+gidse+" "+idse);
		deportService.updateQandT(quatitys, desctext, gidse, idse);
		return idse;
	}
	
	/**
	 * 分页查询
	 */
	@PostMapping("/depot/instockes")
	@ResponseBody
	public String getInstockes(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectInstockesByPagerSql("", "", "", 1, 10, uid);
		Times.formatDate(list,"tdate");
		/*list.get(0).put("wcx", 12);
		list.get(0).getBigInteger("wcx");
*/
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * instockes2分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/depot/instockes2")
	@ResponseBody
	public String getInstockes2(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectInstockesByPagerSql(selects, selectinput, selectend, currentPage, pagesize, uid);

		Times.formatDate(list,"tdate");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

	
		return json;
	}
	
	/**
	 * 访问地址http://localhost:8089/instockesdetail
	 * 跳转到编辑入库明细列表页面
	 * @return
	 *
	 */
	@RequestMapping("/instockesdetail")
	public ModelAndView instockdetail(Integer ids,HttpSession session) {
		System.out.println("进入instockdetail页面");
		ModelAndView mv = new ModelAndView("depot/instockesdetail");
		//查询对应的仓库
		int uid = (int) session.getAttribute("useid");
		mv.addObject("shelfs", deportService.findDeportShelf(uid));
		
		
		JSONObject findInstock = deportService.findInstock(ids);
		List<JSONObject> list = new ArrayList<>();
		list.add(findInstock);

		Times.formatDate(list,"tdate");
		Times.formatDate(list,"Idate");
		mv.addObject("textInstock", list.get(0));
		
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		if (cart == null) {
			cart = new CartBox();
		}
		deportService.findinstockDetail(ids, cart);
		session.setAttribute("ingInOid", ids);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		//System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		session.setAttribute("CartBoxInAll", cart);
		session.setAttribute("CartBoxInAllJson", jsonObj);
		
		return mv;
	}
	
	/**
	 * 跳转到入库明细界面
	 * 
	 * @param oid
	 * @param session
	 * @return
	 */
	@RequestMapping("instockdetail")
	public ModelAndView goodsMx(Integer oid, HttpSession session) {
		System.out.println("oid" + oid);
		ModelAndView mv = new ModelAndView("depot/shozhongorderdetail");
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		if (cart == null) {
			cart = new CartBox();
		}
		deportService.findinstockDetail(oid, cart);
		System.out.println("刚开始的入库单：" + cart);
		session.setAttribute("ingInOid", oid);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		session.setAttribute("CartBoxInAll", cart);
		session.setAttribute("CartBoxInAllJson", jsonObj);
		mv.addObject("oids", oid);
		return mv;
	}
	
	
	/**
	 * 添加一个商品到订单明细中
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("addGoodToInCart")
	@ResponseBody
	public CartBox addGoods(int ids, HttpSession session) {
		System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		if (cart == null) {
			cart = new CartBox();
		}
		cart.addProduct(goodsService.findByGoodsIdByVo(ids));
		// System.out.println("当前购物车...：" + cart.getMap());
		System.out.println("service");
		session.setAttribute("CartBoxInAll", cart);
		return cart;
	}
	
	/**
	 * 修改商品数量
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 */
	@PostMapping("updateGoodToInCart")
	@ResponseBody
	public CartBox updateGoodsQuatitys(int ids, int quatitys, HttpSession session) {

		System.out.println("oid：" + ids + " quatitys：" + quatitys);
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		cart.modifyproductQuatity(ids, quatitys, cart);

		System.out.println("修改后当前购物车：" + cart.getMap());
		session.setAttribute("CartBoxInAll", cart);
		return cart;
	}
	
	/**
	 * 修改商品备注
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 */
	@PostMapping("updescGoodToInCart")
	@ResponseBody
	public CartBox updescGoodToInCart(int ids, String desc, HttpSession session) {
		System.out.println("修改商品备注：");
		System.out.println("oid：" + ids + " desc：" + desc);
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		cart.modifyproductDesc(ids, desc, cart);

		System.out.println("修改后当前购物车：" + cart.getMap());
		session.setAttribute("CartBoxInAll", cart);
		return cart;
	}
	
	/**
	 * 删除商品信息
	 * 
	 * @param ids
	 * @param session
	 * @return
	 */
	@PostMapping("deleteGoodToInCart")
	@ResponseBody
	public CartBox deleteGoods(int ids, HttpSession session) {
		System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		cart.deleteProduct(ids, cart);
		System.out.println("当前购物车：" + cart.getMap());
		session.setAttribute("CartBoxInAll", cart);
		return cart;
	}
	
	
	/**
	 * 提交订单明细 并跳转回订单详情页面
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 * @return
	 */
	@PostMapping("submitGoodsInDetails")
	@ResponseBody
	public JSONObject submitGoods(HttpSession session) {
		CartBox cart = (CartBox) session.getAttribute("CartBoxInAll");
		Integer oid = (Integer) session.getAttribute("ingInOid");
		System.out.println("当前购物车有：" + cart);
		System.out.println("当前订单id是:" + oid);
		deportService.deleteInStockDetail(oid);// 删除对应订单id的订单详情
		deportService.addOrderDetail(oid, cart);// 添加对应订单id的订单详情
		JSONObject object = new JSONObject();
		object.put("odnum", oid);
		//session.removeAttribute("ingInOid");
		session.removeAttribute("CartBoxInAll");
		return object;
	}

	/**
	 *  撤销订单
	 * @param ids
	 * @param session
	 * @return
	 */
	@PostMapping("/depot/instockesdetailreturn")
	@ResponseBody
	public JSONObject instockesdetailreturn(int ids,HttpSession session) {
		JSONObject object = new JSONObject();
		System.out.println("过来撤销了");
		deportService.returnScheduleform(ids);
		return object;
	}


	////////////////////////////
	////////////////////////出库
	/**
	 * 访问地址http://localhost:8089/treasury
	 * 跳转到出库列表页面
	 * @return
	 *
	 */
	@RequestMapping("/treasury")
	public String treasury(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		System.out.println("进入treasury页面");
		return "depot/treasury";
	}
	
	/**
	 * 分页查询
	 */
	@PostMapping("/depot/treasuryes")
	@ResponseBody
	public String getTreasury(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		//int uid = 1;
		List<JSONObject> list = deportService.selectTreasiruByPagerSql("", "", "", 1, 10, uid);
		Times.formatDate(list,"tdate");

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * treasuryes2分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/depot/treasuryes2")
	@ResponseBody
	public String getTreasury2(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectTreasiruByPagerSql(selects, selectinput, selectend, currentPage, pagesize, uid);

		Times.formatDate(list,"tdate");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		for (JSONObject jsonObject : list) {
			System.out.println(jsonObject);
		}
		return json;
	}
	
	@PostMapping("/depot/okinstock")
	@ResponseBody
	public Integer okInstock(Integer id) {
		System.out.println("进入确认入库.");
		deportService.okinstock(id);
		return id;
	}
	
	/**
	 * 出库出库单
	 */
	@PostMapping("/depot/deltreasury")
	@ResponseBody
	public String deltreasury(Integer id,HttpSession session) {
		System.out.println("出库出库单");
		int uid = (int) session.getAttribute("useid");
		deportService.del(id,Integer.parseInt(uid+""));
		return "1";
	}
	
	
	
////////////////////////////
////////////////////////入库流水
	/**
	 * 访问地址http://localhost:8089/inoutlife
	 * 跳转到出入库流水列表页面
	 * @return
	 *
	 */
	@RequestMapping("/inoutlife")
	public String inoutlife(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		System.out.println("进入inoutlife页面");
		return "depot/inoutlife";
	}
	
	/**
	 * 分页查询
	 */
	@PostMapping("/depot/inoutlife")
	@ResponseBody
	public String getInoutlife(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		//int uid = 1;
		List<JSONObject> list = deportService.selectInoutlifeByPagerSql("", "", "", 1, 10, uid);
		Times.formatDate(list,"oitime");
		//根据每条信息修改其出库和入库的对应信息
		deportService.updateInoutlifeByPagerSql(list);

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}
	
	/**
	 * inoutlife2分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/depot/inoutlife2")
	@ResponseBody
	public String getInoutlife2(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		//int uid = 1;
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = deportService.selectInoutlifeByPagerSql(selects, selectinput, selectend, currentPage, pagesize, uid);

		Times.formatDate(list,"oitime");
		//根据每条信息修改其出库和入库的对应信息
		deportService.updateInoutlifeByPagerSql(list);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

	
		return json;
	}
	
	
}
